import 'dart:convert';

import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/utils/sp_utils.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/app_version.dart';
import 'package:flutterlib/model/interest/interest_bean.dart';
import 'package:thrio/thrio.dart';

const String KEY_INTEREST = "KEY_INTEREST";
const String KEY_APPVERSION = "KEY_APPVERSION";


// void setInterests(str){
//
//   // String jsonStr = SpUtils.getString(KEY_INTEREST, defValue: "");
//   //如果之前已经保存了，
//   // if(jsonStr.isEmpty){
//     SpUtils.putString(KEY_INTEREST, str);
//     List<dynamic> decode = json.decode(str);
//     var list = decode.map((dynamic i) => InterestBean.fromJson(i)).toList();
//     final SettingsStore store =
//     Provider.of<SettingsStore>(currentContext, listen: false);
//     store.setInterest(list);
//   // }
// }

List<InterestBean> get allInterestsFromSP{
  var data = SpUtils.getString(KEY_INTEREST);
  // trueDebugPrint("flutterting" + "get allinterest from sp" + data);
  List<dynamic> decode = json.decode(data);
  var list = decode.map((dynamic i) => InterestBean.fromJson(i)).toList();
  return list;
}

InterestBean getInterestFromId(id){
  return allInterestsFromSP.filter((element) => element.id == id).firstOrNull;
}

AppVersion get appVersion{
  var data = SpUtils.getString(KEY_APPVERSION);
  trueDebugPrint("flutterchannel" + "get appversion from sp" + data);
  var appVersion = AppVersion.fromJson(json.decode(data));
  return appVersion;
}

setInterests(str){
  trueDebugPrint("flutterchannel" + "put allinterest to sp");
  SpUtils.putString(KEY_INTEREST, str);
}

setAppVersion(str){
  trueDebugPrint("flutterchannel" + "put appversion to sp" + str);
  SpUtils.putString(KEY_APPVERSION, str);
}
class ConfigDataCenter {
  ConfigDataCenter._();

  static ConfigDataCenter _configDataCenter = ConfigDataCenter._();

  factory ConfigDataCenter() {
    return _configDataCenter;
  }
}
